﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SRM;
using SRM.DataObjects;
using System.Data;

public partial class UserControls_NationalServiceUserControl : System.Web.UI.UserControl, SRM.IUserInput
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Help.BindSapCodeToComboBox(nsstatus_combobox, SRM_Code.GetNSStatus("1"));
            Help.BindSapCodeToComboBox(nsservicetypecode_combobox, SRM_Code.GetServiceType("1"));
            Help.BindSapCodeToComboBox(ns_combobox, SRM_Code.GetMilitaryStatus("1"));
            Help.BindSapCodeToComboBox(rsservicetypecode_combobox, SRM_Code.GetServiceType("1"));

            string applicantKey = string.Empty;
            applicantKey = ((IApplicantForm)Page).ApplicantKey;
            DataTable dt = SRM_Applicant.GetNationalService(applicantKey);
            if ((dt != null) && (dt.Rows.Count > 0))
            {
                System.Data.DataRow row = dt.Rows[0];
                Help.SetComboBoxSelectedValue(nsstatus_combobox, row.Field<string>("NSStatus"));
                Help.SetComboBoxSelectedValue(ns_combobox, row.Field<string>("NS"));

                if ((nsstatus_combobox.SelectedValue == "DF") ||
                    (nsstatus_combobox.SelectedValue == "EX") ||
                    (nsstatus_combobox.SelectedValue == "NA") ||
                    (nsstatus_combobox.SelectedValue == ""))
                {
                    SetDependentDisplay(false);
                }
                else
                {
                    SetDependentDisplay(true);

                    // Bind values to the following fields
                    //NSServiceTypeCode,
                    //NSVocation,
                    //NSServiceRankCode,
                    //NSEnlistmentDate,
                    //NSORDDAte,

                    Help.SetComboBoxSelectedValue(nsservicetypecode_combobox, row.Field<string>("NSServiceTypeCode"));

                    Help.BindSapCodeToComboBox(nsvocation_combobox, SRM_Code.GetNSVocation("1", nsservicetypecode_combobox.SelectedValue));
                    Help.SetComboBoxSelectedValue(nsvocation_combobox, row.Field<string>("NSVocation"));

                    Help.BindSapCodeToComboBox(nsservicerankcode_combobox, SRM_Code.GetNSRank("1", nsservicetypecode_combobox.SelectedValue));
                    Help.SetComboBoxSelectedValue(nsservicerankcode_combobox, row.Field<string>("NSServiceRankCode"));

                    nsenlistmentdate_datepicker.SelectedDate = row.Field<DateTime?>("NSEnlistmentDate");
                    nsorddate_datepicker.SelectedDate = row.Field<DateTime?>("NSORDDate");
                }

                if ((ns_combobox.SelectedValue == "1") || (ns_combobox.SelectedValue == "3") || (ns_combobox.SelectedValue == ""))
                {
                    SetReservistDependentDisplay(false);
                }
                else
                {
                    SetReservistDependentDisplay(true);

                    // Bind values to the following fields
                    
                    //RSServiceTypeCode,
                    //RSVocation,
                    //RSServiceRankCode,
                    //RSCompletionDate

                    

                    Help.SetComboBoxSelectedValue(rsservicetypecode_combobox, row.Field<string>("RSServiceTypeCode"));

                    Help.BindSapCodeToComboBox(rsvocation_combobox, SRM_Code.GetNSVocation("1", rsservicetypecode_combobox.SelectedValue));
                    Help.SetComboBoxSelectedValue(rsvocation_combobox, row.Field<string>("RSVocation"));

                    Help.BindSapCodeToComboBox(rsservicerankcode_combobox, SRM_Code.GetNSRank("1", rsservicetypecode_combobox.SelectedValue));
                    Help.SetComboBoxSelectedValue(rsservicerankcode_combobox, row.Field<string>("RSServiceRankCode"));

                    rscompletiondate_datepicker.SelectedDate = row.Field<DateTime?>("RSCompletionDate");
                }

            }
        }
    }

    public void SaveUserInput()
    {
        string applicantKey = string.Empty;

        if (Page is IApplicantForm)
        {
            applicantKey = ((IApplicantForm)Page).ApplicantKey;

            /*
             * 
                    NSStatus,
                    NSServiceTypeCode,
                    NSVocation,
                    NSServiceRankCode,
                    NSEnlistmentDate,

                    NS,
                    NSORDDAte,
                    RSServiceTypeCode,
                    RSVocation,
                    RSServiceRankCode,
                    RSCompletionDate
             * */

            SRM_Applicant.UpdateNationalService(applicantKey,
                nsstatus_combobox.SelectedValue,
                nsservicetypecode_combobox.SelectedValue,
                nsvocation_combobox.SelectedValue,
                nsservicerankcode_combobox.SelectedValue,
                nsenlistmentdate_datepicker.SelectedDate, 
                ns_combobox.SelectedValue,
                nsorddate_datepicker.SelectedDate, 
                rsservicetypecode_combobox.SelectedValue, 
                rsvocation_combobox.SelectedValue, 
                rsservicerankcode_combobox.SelectedValue,
                rscompletiondate_datepicker.SelectedDate
                );
        }
    }

    public void SetDependentDisplay(bool bln)
    {
        service_type_row.Visible = bln;
        vocation_row.Visible = bln;
        rank_row.Visible = bln;
        enlistment_date_row.Visible = bln;
        ord_date_row.Visible = bln;
    }

    public void SetReservistDependentDisplay(bool bln)
    {
        rs_service_type.Visible = bln;
        rs_vocation.Visible = bln;
        rs_rank.Visible = bln;
        rs_completiondate.Visible = bln;
    }

    protected void nsstatus_combobox_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        //if ((ns_combobox.SelectedValue == "1") || (ns_combobox.SelectedValue == "3") || (ns_combobox.SelectedValue == ""))
        if ((nsstatus_combobox.SelectedValue == "DF") ||
            (nsstatus_combobox.SelectedValue == "EX") ||
            (nsstatus_combobox.SelectedValue == "NA") ||
            (nsstatus_combobox.SelectedValue == string.Empty))
        {
            SetDependentDisplay(false);
        }
        else
        {
            SetDependentDisplay(true);
        }
    }

    protected void ns_combobox_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        if ((ns_combobox.SelectedValue == "1") || (ns_combobox.SelectedValue == "3") || (ns_combobox.SelectedValue == ""))
        {
            SetReservistDependentDisplay(false);
        }
        else
        {
            SetReservistDependentDisplay(true);
        }
    }

    protected void nsservicetypecode_combobox_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        Help.BindSapCodeToComboBox(nsvocation_combobox, SRM_Code.GetNSVocation("1", nsservicetypecode_combobox.SelectedValue));
        Help.BindSapCodeToComboBox(nsservicerankcode_combobox, SRM_Code.GetNSRank("1", nsservicetypecode_combobox.SelectedValue));
    }

    protected void rsservicetypecode_combobox_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        Help.BindSapCodeToComboBox(rsvocation_combobox, SRM_Code.GetNSVocation("1", rsservicetypecode_combobox.SelectedValue));
        Help.BindSapCodeToComboBox(rsservicerankcode_combobox, SRM_Code.GetNSRank("1", rsservicetypecode_combobox.SelectedValue));
    }
}
